home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / dix / cursor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-14  |  8.7 KB  |  363 lines

  1. /***********************************************************
  2. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25.  
  26. /* $XConsortium: cursor.c,v 1.38 89/07/16 17:24:22 rws Exp $ */
  27.  
  28. #include "X.h"
  29. #include "Xmd.h"
  30. #include "scrnintstr.h"
  31. #include "dixstruct.h"
  32. #include "cursorstr.h"
  33. #include "dixfontstr.h"
  34. #include "opaque.h"
  35.  
  36. typedef struct _GlyphShare {
  37.     FontPtr font;
  38.     unsigned short sourceChar;
  39.     unsigned short maskChar;
  40.     CursorBitsPtr bits;
  41.     struct _GlyphShare *next;
  42. } GlyphShare, *GlyphSharePtr;
  43.  
  44. static GlyphSharePtr sharedGlyphs = (GlyphSharePtr)NULL;
  45.  
  46. static void
  47. FreeCursorBits(bits)
  48.     CursorBitsPtr bits;
  49. {
  50.     if (--bits->refcnt > 0)
  51.     return;
  52.     xfree(bits->source);
  53.     xfree(bits->mask);
  54.     if (bits->refcnt == 0)
  55.     {
  56.     register GlyphSharePtr *prev, this;
  57.  
  58.     for (prev = &sharedGlyphs;
  59.          (this = *prev) && (this->bits != bits);
  60.          prev = &this->next)
  61.         ;
  62.     if (this)
  63.     {
  64.         *prev = this->next;
  65.         CloseFont(this->font, (Font)0);
  66.         xfree(this);
  67.     }
  68.     xfree(bits);
  69.     }
  70. }
  71.  
  72. /*
  73.  * To be called indirectly by DeleteResource; must use exactly two args
  74.  */
  75. /*ARGSUSED*/
  76. int
  77. FreeCursor( pCurs, cid)
  78.     CursorPtr     pCurs;
  79.     Cursor     cid;    
  80. {
  81.     int        nscr;
  82.  
  83.     ScreenPtr    pscr;
  84.  
  85.     if ( --pCurs->refcnt > 0)
  86.     return(Success);
  87.  
  88.     for (nscr = 0; nscr < screenInfo.numScreens; nscr++)
  89.     {
  90.     pscr = screenInfo.screens[nscr];
  91.     (void)( *pscr->UnrealizeCursor)( pscr, pCurs);
  92.     }
  93.     FreeCursorBits(pCurs->bits);
  94.     xfree( pCurs);
  95.     return(Success);
  96. }
  97.  
  98. /*
  99.  * does nothing about the resource table, just creates the data structure.
  100.  * does not copy the src and mask bits
  101.  */
  102. CursorPtr 
  103. AllocCursor(psrcbits, pmaskbits, cm,
  104.         foreRed, foreGreen, foreBlue, backRed, backGreen, backBlue)
  105.     unsigned char *    psrcbits;        /* server-defined padding */
  106.     unsigned char *    pmaskbits;        /* server-defined padding */
  107.     CursorMetricPtr    cm;
  108.     unsigned        foreRed, foreGreen, foreBlue;
  109.     unsigned        backRed, backGreen, backBlue;
  110. {
  111.     CursorBitsPtr  bits;
  112.     CursorPtr     pCurs;
  113.     int        nscr;
  114.     ScreenPtr     pscr;
  115.  
  116.     pCurs = (CursorPtr)xalloc(sizeof(CursorRec) + sizeof(CursorBits));
  117.     if (!pCurs)
  118.     return (CursorPtr)NULL;
  119.     bits = (CursorBitsPtr)((char *)pCurs + sizeof(CursorRec));
  120.     bits->source = psrcbits;
  121.     bits->mask = pmaskbits;
  122.     bits->width = cm->width;
  123.     bits->height = cm->height;
  124.     bits->xhot = cm->xhot;
  125.     bits->yhot = cm->yhot;
  126.     bits->refcnt = -1;
  127.  
  128.     pCurs->bits = bits;
  129.     pCurs->refcnt = 1;        
  130.  
  131.     pCurs->foreRed = foreRed;
  132.     pCurs->foreGreen = foreGreen;
  133.     pCurs->foreBlue = foreBlue;
  134.  
  135.     pCurs->backRed = backRed;
  136.     pCurs->backGreen = backGreen;
  137.     pCurs->backBlue = backBlue;
  138.  
  139.     /*
  140.      * realize the cursor for every screen
  141.      */
  142.     for (nscr = 0; nscr < screenInfo.numScreens; nscr++)
  143.     {
  144.     pscr = screenInfo.screens[nscr];
  145.         if (!( *pscr->RealizeCursor)( pscr, pCurs))
  146.     {
  147.         while (--nscr >= 0)
  148.         {
  149.         pscr = screenInfo.screens[nscr];
  150.         ( *pscr->UnrealizeCursor)( pscr, pCurs);
  151.         }
  152.         xfree(pCurs);
  153.         return (CursorPtr)NULL;
  154.     }
  155.     }
  156.     return pCurs;
  157. }
  158.  
  159. int
  160. AllocGlyphCursor(source, sourceChar, mask, maskChar,
  161.          foreRed, foreGreen, foreBlue, backRed, backGreen, backBlue,
  162.          ppCurs, client)
  163.     Font source, mask;
  164.     unsigned short sourceChar, maskChar;
  165.     unsigned foreRed, foreGreen, foreBlue;
  166.     unsigned backRed, backGreen, backBlue;
  167.     CursorPtr *ppCurs;
  168.     ClientPtr client;
  169. {
  170.     FontPtr  sourcefont, maskfont;
  171.     unsigned char   *srcbits;
  172.     unsigned char   *mskbits;
  173.     CursorMetricRec cm;
  174.     int res;
  175.     CursorBitsPtr  bits;
  176.     CursorPtr     pCurs;
  177.     int        nscr;
  178.     ScreenPtr     pscr;
  179.     GlyphSharePtr pShare;
  180.  
  181.     sourcefont = (FontPtr) LookupIDByType(source, RT_FONT);
  182.     maskfont = (FontPtr) LookupIDByType(mask, RT_FONT);
  183.  
  184.     if (!sourcefont)
  185.     {
  186.     client->errorValue = source;
  187.     return(BadFont);
  188.     }
  189.     if (!maskfont && (mask != None))
  190.     {
  191.     client->errorValue = mask;
  192.     return(BadFont);
  193.     }
  194.     if (sourcefont != maskfont)
  195.     pShare = (GlyphSharePtr)NULL;
  196.     else
  197.     {
  198.     for (pShare = sharedGlyphs;
  199.          pShare &&
  200.          ((pShare->font != sourcefont) ||
  201.           (pShare->sourceChar != sourceChar) ||
  202.           (pShare->maskChar != maskChar));
  203.          pShare = pShare->next)
  204.         ;
  205.     }
  206.     if (pShare)
  207.     {
  208.     pCurs = (CursorPtr)xalloc(sizeof(CursorRec));
  209.     if (!pCurs)
  210.         return BadAlloc;
  211.     bits = pShare->bits;
  212.     bits->refcnt++;
  213.     }
  214.     else
  215.     {
  216.     if (!CursorMetricsFromGlyph(sourcefont, sourceChar, &cm))
  217.     {
  218.         client->errorValue = sourceChar;
  219.         return BadValue;
  220.     }
  221.     if (!maskfont)
  222.     {
  223.         register long n;
  224.         register unsigned char *bits;
  225.  
  226.         n = PixmapBytePad(cm.width, 1)*(long)cm.height;
  227.         bits = mskbits = (unsigned char *)xalloc(n);
  228.         if (!bits)
  229.         return BadAlloc;
  230.         while (--n >= 0)
  231.         *bits++ = ~0;
  232.     }
  233.     else
  234.     {
  235.         if (!CursorMetricsFromGlyph(maskfont, maskChar, &cm))
  236.         {
  237.         client->errorValue = maskChar;
  238.         return BadValue;
  239.         }
  240.         if (res = ServerBitsFromGlyph(maskfont, maskChar, &cm, &mskbits))
  241.         return res;
  242.     }
  243.     if (res = ServerBitsFromGlyph(sourcefont, sourceChar, &cm, &srcbits))
  244.     {
  245.         xfree(mskbits);
  246.         return res;
  247.     }
  248.     if (sourcefont != maskfont)
  249.     {
  250.         pCurs = (CursorPtr)xalloc(sizeof(CursorRec) + sizeof(CursorBits));
  251.         if (pCurs)
  252.         bits = (CursorBitsPtr)((char *)pCurs + sizeof(CursorRec));
  253.         else
  254.         bits = (CursorBitsPtr)NULL;
  255.     }
  256.     else
  257.     {
  258.         pCurs = (CursorPtr)xalloc(sizeof(CursorRec));
  259.         if (pCurs)
  260.         bits = (CursorBitsPtr)xalloc(sizeof(CursorBits));
  261.         else
  262.         bits = (CursorBitsPtr)NULL;
  263.     }
  264.     if (!bits)
  265.     {
  266.         xfree(pCurs);
  267.         xfree(mskbits);
  268.         xfree(srcbits);
  269.         return BadAlloc;
  270.     }
  271.     bits->source = srcbits;
  272.     bits->mask = mskbits;
  273.     bits->width = cm.width;
  274.     bits->height = cm.height;
  275.     bits->xhot = cm.xhot;
  276.     bits->yhot = cm.yhot;
  277.     if (sourcefont != maskfont)
  278.         bits->refcnt = -1;
  279.     else
  280.     {
  281.         bits->refcnt = 1;
  282.         pShare = (GlyphSharePtr)xalloc(sizeof(GlyphShare));
  283.         if (!pShare)
  284.         {
  285.         FreeCursorBits(bits);
  286.         return BadAlloc;
  287.         }
  288.         pShare->font = sourcefont;
  289.         sourcefont->refcnt++;
  290.         pShare->sourceChar = sourceChar;
  291.         pShare->maskChar = maskChar;
  292.         pShare->bits = bits;
  293.         pShare->next = sharedGlyphs;
  294.         sharedGlyphs = pShare;
  295.     }
  296.     }
  297.     pCurs->bits = bits;
  298.     pCurs->refcnt = 1;
  299.  
  300.     pCurs->foreRed = foreRed;
  301.     pCurs->foreGreen = foreGreen;
  302.     pCurs->foreBlue = foreBlue;
  303.  
  304.     pCurs->backRed = backRed;
  305.     pCurs->backGreen = backGreen;
  306.     pCurs->backBlue = backBlue;
  307.  
  308.     /*
  309.      * realize the cursor for every screen
  310.      */
  311.     for (nscr = 0; nscr < screenInfo.numScreens; nscr++)
  312.     {
  313.     pscr = screenInfo.screens[nscr];
  314.         if (!( *pscr->RealizeCursor)( pscr, pCurs))
  315.     {
  316.         while (--nscr >= 0)
  317.         {
  318.         pscr = screenInfo.screens[nscr];
  319.         ( *pscr->UnrealizeCursor)( pscr, pCurs);
  320.         }
  321.         FreeCursorBits(pCurs->bits);
  322.         xfree(pCurs);
  323.         return BadAlloc;
  324.     }
  325.     }
  326.     *ppCurs = pCurs;
  327.     return Success;
  328. }
  329.  
  330. /***********************************************************
  331.  * CreateRootCursor
  332.  *
  333.  * look up the name of a font
  334.  * open the font
  335.  * add the font to the resource table
  336.  * make a cursor from the glyphs
  337.  * add the cursor to the resource table
  338.  *************************************************************/
  339.  
  340. CursorPtr 
  341. CreateRootCursor(pfilename, glyph)
  342.     char *        pfilename;
  343.     unsigned short    glyph;
  344. {
  345.     CursorPtr     curs;
  346.     FontPtr     cursorfont;
  347.     XID        fontID;
  348.  
  349.     fontID = FakeClientID(0);
  350.     cursorfont = OpenFont( (unsigned)strlen( pfilename), pfilename);
  351.     if (!cursorfont || !AddResource(fontID, RT_FONT, (pointer)cursorfont))
  352.     return NullCursor;
  353.  
  354.     if (AllocGlyphCursor(fontID, glyph, fontID, glyph + 1,
  355.              0, 0, 0, ~0, ~0, ~0, &curs, serverClient) != Success)
  356.     return NullCursor;
  357.  
  358.     if (!AddResource(FakeClientID(0), RT_CURSOR, (pointer)curs))
  359.     return NullCursor;
  360.  
  361.     return curs;
  362. }
  363.